using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.Security.Permissions;
using System.IO;
using System.Security.Cryptography.X509Certificates;

//
//  Software written by Rodney Viana @ Integrated Services Inc (c) 2008
//  Please reach us @ www.isi85.com
//


namespace MOSSLoadTest
{
    static class Certificate
    {
        private static X509Certificate2Collection certCollection = null;

        private static bool isSelected = false;

        public static bool IsSelected
        {
            get
            {
                return isSelected;
            }
        }

        public static X509Certificate2Collection Certificates
        {
            get
            {
                return certCollection;
            }
        }
        
        public static bool SelectCertificate()
        {
            try
            {
                X509Store store = new X509Store("MY", StoreLocation.CurrentUser);
                store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
                X509Certificate2Collection collection = (X509Certificate2Collection)store.Certificates;
                X509Certificate2Collection fcollection = (X509Certificate2Collection)collection.Find(X509FindType.FindByTimeValid, DateTime.Now, false);
                certCollection = X509Certificate2UI.SelectFromCollection(fcollection, "Certification Select", "Please select a certificate from the following list", X509SelectionFlag.MultiSelection);
                isSelected = (certCollection.Count > 0);
                store.Close();
                return isSelected;
            }
            catch (CryptographicException)
            {
                Console.WriteLine("Information could not be written out for this certificate.");
            }
            return false;
        }        

    }
}
